<template>
  <base-breadcrumb :breadcrumb="!isAudit">
    <div class="about-master-container">
      <b-steps :current="stepVal" direction="vertical" @change.self="stepChange">
        <b-step title="基本信息">
          <a-card v-show="activeKey[0]" slot="description" v-loading="loading" :bordered="false">
            <BForm ref="form" :form="formData" :config="formConfig" :rule="rules" />
            <div v-if="!disabled" class="pub-next-btn">
              <a-button type="primary" @click="submit('DRAFT')">保存草稿</a-button>
              <a-button type="primary" @click="submit('DRAFT',true)">下一步</a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="现场成本节超分析表明细">
          <a-card v-if="activeKey[1]" slot="description" :bordered="false">
            <childDetail :id="formData.id" :disabled="disabled" />
            <div v-if="!disabled" class="pub-next-btn">
              <a-button type="primary" @click="previous()">上一步</a-button>
              <a-button type="primary" @click="next()">下一步</a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="相关附件">
          <a-card v-if="activeKey[2]" slot="description" :bordered="false">
            <u-table
              :upload-list.sync="addFile"
              :delete-url="fileDel"
              :is-show="!disabled"
              :list-url="fileList"
              :business-id="formData.id"
              business-code="MM21"
            />
            <div v-if="!disabled" class="pub-next-btn">
              <a-button type="primary" @click="previous()">上一步</a-button>
            </div>
          </a-card>
        </b-step>
      </b-steps>
      <div class="pub-header-btn page-btn-right-top">
        <template v-if="!isAudit">
          <a-button v-if="!disabled" type="primary" :loading="loading" @click="submit('SUBMITTED')">
            提交
          </a-button>
          <BasePrintButton
            v-if="formData.id && disabled"
            :before-print-fn="beforePrintFn"
            style="display: inline-block"
          />
        </template>
        <a-button v-checkVisibleInDetail @click="$close">关闭</a-button>
      </div>
    </div>
    <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
  </base-breadcrumb>
</template>
<script>
import SelectDownProject from '@/components/Select/SelectDownProject'
import childDetail from './childDetail'
import Api from '@/api/material/benefitAnalysis/fieldCostAnalysis'
import moment from 'moment'
import { async } from 'regenerator-runtime'

export default {
  name: 'a' + Date.now(),
  components: {
    childDetail
  },
  props: {
    isAudit: { // 是否作为审批详情
      type: Boolean,
      default: false
    }
  },
  watch: {
    isAudit: {
      immediate: true,
      handler(val) {
        if (val) {
          this.disabled = true
        }
      }
    },
    stepVal(newVal, oldVal) {
      this.$set(this.activeKey, newVal, true)
    }
  },
  computed: {
    formConfig() {
      return [
        {
          row: true,
          children: [
            { type: 'input', value: 'code', label: '分析表编号', attrs: { disabled: true } },
            // {
            //   type: SelectProject,
            //   value: 'projectId',
            //   label: '项目名称',
            //   attrs: {
            //     form: this.formData,
            //     keyValue: 'branchComId'
            //   },
            //   on: {
            //     selectChange: e => this.formData.projectName = e.name
            //   },
            //   show: !this.disabled
            // },
            {
              type: SelectDownProject,
              value: 'projectId',
              label: '项目名称',
              // attrs: {
              //   form: this.formData,
              //   keyValue: 'branchComId'
              // },
              on: {
                selectChange: e => {
                  this.formData.projectName = e.projectName
                }
              },
              show: !this.disabled
            },
            {
              type: 'input',
              value: 'projectName',
              label: '项目名称',
              attrs: { disabled: this.disabled },
              show: this.disabled
            },
            { type: 'input', value: 'number', label: '单据编号', show: !this.disabled },
            { type: 'input', value: 'numberAll', label: '单据编号', attrs: { disabled: true }, show: this.disabled },
            {
              type: 'date-picker',
              value: 'startDate',
              label: '开始时间',
              attrs: { disabled: this.disabled, valueFormat: 'YYYY-MM-DD HH:mm:SS' }
            },
            {
              type: 'date-picker',
              value: 'endDate',
              label: '截止时间',
              attrs: { disabled: this.disabled, valueFormat: 'YYYY-MM-DD HH:mm:SS', disabledDate: this.disabledDate }
            },
            { type: 'input', value: 'creatorName', label: '创建人', attrs: { disabled: true } },
            { type: 'input', value: 'createTime', label: '创建时间', attrs: { disabled: true } }
          ]
        }
      ]
    }
  },
  data() {
    return {
      addFile: Api.addFile(),
      fileDel: Api.deleteFile(),
      fileList: Api.findFileList(),
      beforePrintFn: async () => {
        let res = await Api.print({ id: this.formData.id })
        return res.data.content
      },
      activeKey: [true, false, false, false, false, false, false],
      loading: false,
      disabled: this.$route.query.pageType === 'look',
      stepVal: 0,
      formData: {
        id: null,
        createTime: this.getNowTime(),
        creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name,
        branchComId: this.$currentPost().orgId,
        branchComName: this.$currentPost().orgName,
        startDate: null
        // endDate: null,
      },
      rules: {
        projectId: { required: true, message: '请选择项目名称', trigger: 'change' },
        startDate: { required: true, message: '请选择开始时间', trigger: 'change' },
        endDate: { required: true, message: '请选择截止时间', trigger: 'change' },
        number: { required: true, message: '请填写单据编号', trigger: 'blur' }
      },
      isNext: false,
       //获取流程配置值
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      businessId: null
    }
  },
  mounted: async function() {
    if (this.$route.query.id) {
      this.formData.id = this.$route.query.id
      await this.getEditInfo()
    }
    if (this.disabled) {
      this.activeKey = [true, true, true, false, false, false, false]
    }
      //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('mm_scene_cost_analysis')
  },
  methods: {
    disabledDate(current) {
      if (this.formData.startDate) {
        let startDate
        if (typeof this.formData.startDate === 'string') {
          startDate = moment(this.formData.startDate)
        } else {
          startDate = this.formData.startDate
        }
        return current && current < moment(this.formData.startDate)
      }
      return true
    },
    previous: function() {
      this.isNext = true
      this.stepVal--
      if (this.stepVal < 0) {
        this.stepVal = 0
      }
    },
    stepChange(val) {
      if (!this.formData.id && !this.isAudit) {
        if (val) {
          this.$message.warning('切换步骤前请先填写基本信息')
          this.stepVal = 0
        }
      } else {
        if (!this.isNext) {
          this.$set(this.activeKey, val, !this.activeKey[val])
        }
      }
      this.isNext = false
    },
    // 获取编辑回显数据
    async getEditInfo() {
      this.loading = true
      let res = await Api.findListByMainId({ id: this.formData.id })
      this.loading = false
      if (res.code === 200) {
        this.formData = this.$assign(this.formData, res.data)
      }
    },
    async check() {
      return new Promise(resolve => {
        this.$refs.form.validate(async valid => {
          if (valid) {
            resolve(true)
          } else {
            this.$noticeWarning('请填写完整')
          }
        })
      })
    },
    async submit(status, isNext = false) {
      this.setProcessConfig = null;
      if (status !== 'DRAFT') {
        if (!await this.check()) return
      }
      let temp = this.$clone(this.formData)
      temp.auditStatus = status
      let res
      this.loading = true
      this.submitApprovalConfirm(this.isSubmit?this.processConfig:6,async(flag)=>{
        if(!flag) {
            this.loading = false
            return
        }
      if (temp.id) {
        res = await Api.editByMain({}, temp)
      } else {
        res = await Api.createByMain({}, temp)
      }
      this.loading = false
      if (res.code === this.SUCCESS_CODE) {
        this.formData.id = res.data.id
        this.$noticeSuccess('成功')
        if (status === 'SUBMITTED') {
          // this.$close()
          //提交时，返回id
          this.businessId = this.formData.id;
          this.setProcessConfig = this.processConfig;
        }
        this.getEditInfo()
        if (isNext) {
          this.next()
        }
      }
     })
  
    },
    next: function(val) {
      this.isNext = true
      this.stepVal++
    }
  }
}
</script>
